package com.example.jegotrip.fragment.mine;

import androidx.fragment.app.Fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.jegotrip.R;
import com.example.jegotrip.activity.CreateNoteActivity;
import com.example.jegotrip.activity.LoginActivity;
import com.example.jegotrip.base.BaseFragment;
import com.example.jegotrip.db.NoteDBHelper;
import com.example.jegotrip.entity.UserInfo;

public class MineFragment extends BaseFragment {
    private TextView tv_username;
    private TextView tv_user_desc;
    private TextView tv_update_password;
    private TextView tv_clearcache;
    private Button tv_exit_login;

    private UserInfo userInfo;

    private String[] cache_item = new String[]{"确认要清理缓存吗?","编辑","删除"};
    private int currentIndex = 0;

    @Override
    protected void initData() {
        userInfo = UserInfo.getUserInfo();
        tv_username.setText(userInfo.getUsername());
        tv_user_desc.setText(userInfo.getUsername());
    }

    @Override
    protected void setListener() {
        tv_update_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), UpdatePwdActivity.class);
                startActivity(intent);
            }
        });
        tv_clearcache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(getActivity())
                        .setTitle("操作")
                        .setSingleChoiceItems(cache_item, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                currentIndex = i;
                            }
                        })
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                if (currentIndex == 2){
//删除缓存
                                }
                            }
                        })
                        .show();
            }
        });
        tv_exit_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), LoginActivity.class));
            }
        });
    }

    @Override
    protected void initViews() {
        tv_username = rootView.findViewById(R.id.tv_username);
        tv_user_desc = rootView.findViewById(R.id.tv_user_desc);
        tv_update_password = rootView.findViewById(R.id.tv_update_password);
        tv_exit_login = rootView.findViewById(R.id.tv_exit_login);
        tv_clearcache = rootView.findViewById(R.id.tv_clearcache);

    }

    @Override
    protected int getLayoutResOId() {
        return R.layout.fragment_mine;
    }
}

